package com.pms.eap.manager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;

import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.pms.eap.relationship.Party;
import com.pms.eap.sa.User;

@Entity
@DomainModel
@ModelDisplayProperty(value = "name")
@BatchSize(size=10)
public class Department extends Party   {
	private static final long serialVersionUID = -7074473750739345515L;
	
	public Department(){
		super();
	}
	public Department(Integer parentId) {
		super();
		setId(parentId);
	}

	@Transient
	public Collection<Integer> getAllUserIds(){
		List<Department> departments = getDescendentChildren(Department.class);
		if (departments == null){
			departments = new ArrayList<Department>();
		}
		departments.add(this);
		if (departments != null && !departments.isEmpty()){
			Set<Integer> userIds = new HashSet<Integer>(); 
			for(Department dept : departments){
				List<User> users = dept.getChildren(User.class);
				if (users != null && !users.isEmpty()){
					for(User user : users){
						userIds.add(user.getId());
					}
				}
			}
			return userIds;
		}
		return Collections.EMPTY_LIST;
		
	}
	
//	@Override
//	public String[] gatherProperties() {
//		return new String[]{"name"};
//	}

}
